package com.fzebu.fastboot.domain.dto;

import com.alibaba.fastjson.JSON;
import com.fzebu.fastboot.domain.constant.ResultMsg;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;


/**
 * @author Jack
 * @version 1.0.0
 * @create 2020/1/13 17:52
 */
@Setter
@Getter
@ToString
public class ResultDTO {

    private boolean status;

    private int code;

    private String message;

    private Object data;

    public static String fail() {
        return fail(ResultMsg.FAIL);
    }

    public static String fail(ResultMsg msg) {
        return fail(msg, null);
    }

    public static String fail(Object data) {
        return fail(ResultMsg.FAIL, data);
    }

    public static String fail(ResultMsg msg, Object data) {
        return ret(false, msg, data);
    }

    public static String fail(String text, Object data) {
        return ret(false, ResultMsg.FAIL.getCode(), text, data);
    }

    public static String success() {
        return success(null);
    }

    public static String success(Object data) {
        return ret(true, ResultMsg.SUCCESS, data);
    }

    public static String success(String text, Object data) {
        return ret(true, ResultMsg.SUCCESS.getCode(), text, data);
    }

    public static String success(ResultMsg msg, Object data) {
        return ret(true, msg, data);
    }

    private static String ret(boolean status, ResultMsg msg, Object data) {
        return ret(status, msg.getCode(), msg.getText(), data);
    }

    private static String ret(boolean status, int  code, String text, Object data) {
        ResultDTO result = new ResultDTO();
        result.setCode(code);
        result.setMessage(text);
        result.setStatus(status);
        result.setData(data);
        return JSON.toJSONString(result);
    }
}